<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/13 0013
 * Time: 10:12
 */

namespace App\Ordersys\Repositories;


use App\Admin\Models\MemberModel;
use App\Ordersys\Contracts\OrderApplyInterface;
use App\Ordersys\Exceptions\OrdersysException;
use App\Ordersys\Models\AwardLogModel;
use App\Ordersys\Models\OrdersysBalanceLogModel;
use App\Ordersys\Models\OrdersysCustomerModel;
use App\Ordersys\Models\OrdersysOrderExpressModel;
use App\Ordersys\Models\OrdersysOrderLogModel;
use App\Ordersys\Models\OrdersysOrderModel;
use App\Ordersys\Models\OrdersysScoreLogModel;

/**
 * 订单管理接口
 * Class DbOrderApplyRepository
 * @package App\Ordersys\Repositories
 */
class DbOrderApplyRepository implements OrderApplyInterface
{

    /**
     * 订单模型
     * @return OrdersysOrderModel
     */
    public function getOrdersysOrderModel()
    {
        return app()->make(OrdersysOrderModel::class);
    }

    /**
     * @return OrdersysOrderExpressModel
     */
    public function getOrdersysOrderExpressModel(){
        return app()->make(OrdersysOrderExpressModel::class);
    }
    /**
     * 余额模型
     * @return OrdersysBalanceLogModel
     */
    public function getOrdersysBalanceLogModel()
    {
        return app()->make(OrdersysBalanceLogModel::class);
    }

    /**
     * 订单日志模型
     * @return OrdersysOrderLogModel
     */
    public function getOrdersysOrderLogModel()
    {
        return app()->make(OrdersysOrderLogModel::class);
    }

    /**
     * @return MemberModel
     */
    public function getMemberModel(){
        return   app()->make(MemberModel::class);
    }

    /**
     * 会员模型
     * @return OrdersysCustomerModel
     */
    public function getOrdersysCustomerModel(){
        return app()->make(OrdersysCustomerModel::class);
    }

    /**
     * 积分
     * @return mixed
     */
    public function getOrdersysScoreLogModel(){
        return app()->make(OrdersysScoreLogModel::class);
    }

    /**
     * 审核通过
     * @param $data
     * @return mixed|void
     */
    public function applyTrue($data){
        $orderInfo=$this->getOrdersysOrderModel()->newQuery()->where("id",$data["id"])->first();
        if($orderInfo->order_type==1){  //下单定
            if(!$this->getOrdersysOrderModel()->newQuery()->where("id",$orderInfo->id)->update(["apply_status"=>'20',"apply_uid"=>UID])) {  //充值通过后
                throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
            }

            if(!$this->getOrdersysOrderLogModel()->saveData($orderInfo,"10")){
                throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
            }
            if (!empty($orderInfo->pay_uid)) {   //支付定单不为空---用户金额是否不足
                //判断用户金额是否足购
                $customerInfo=$this->getOrdersysCustomerModel()->newQuery()->where("id",$orderInfo->pay_uid)->first();
                if($customerInfo->balance<$orderInfo->paid_amount){  //用户余额如果小于订单金额则报错
                    throw new OrdersysException(OrdersysException::EROOR_BALANCE_AMOUNT_SHORT_NOT);
                }
                $pay_name=$this->getMemberModel()->newQuery()->where("uid",UID)->value("nickname");
                $data = array(
                    'order_id'=>$orderInfo->id,
                    'member_id'=>$orderInfo->pay_uid,
                    'member_name'=>$orderInfo->pay_username,
                    'amount'=>$orderInfo->paid_amount,
                    'sale_uid'=>$orderInfo->sale_id,
                    'sale_name'=>$orderInfo->sale_name,
                    'apply_uid'=>UID,
                    'apply_name'=>$pay_name,
                );
                if(!$this->getOrdersysBalanceLogModel()->balanceLog("consume",$data)){
                    throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
                }
                //积分计算
                $scoreSetting=get_config_param_value("SCORE_SETTING");  //设置的比例
                $pay_name=$this->getMemberModel()->newQuery()->where("uid",UID)->value("nickname");
                $data = array(
                    'order_id'=>$orderInfo->id,
                    'member_id'=>$orderInfo->pay_uid,
                    'member_name'=>$orderInfo->pay_username,
                    'amount'=>intval($orderInfo->paid_amount*$scoreSetting),
                    'sale_uid'=>$orderInfo->sale_id,
                    'sale_name'=>$orderInfo->sale_name,
                    'apply_uid'=>UID,
                    'apply_name'=>$pay_name,
                );
                if(!$this->getOrdersysScoreLogModel()->balanceLog("backscore",$data)){
                    throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
                }
            }
            $adata=array();

            $adata["award_type"]=1;
            $adata["uid"]=$orderInfo->uid;
            $adata["order_id"]=$orderInfo->id;
            $adata['money']= $orderInfo->all_amount;
            $adata["remark"]="支付订单号：".$orderInfo->id.",订单金额:".$orderInfo->all_amount."超过设定金额:";

            $this->getAwardLogModel()->awardLog(1,$adata);
            return array();
        }else{
            if(!$this->getOrdersysOrderModel()->newQuery()->where("id",$orderInfo->id)->update(["apply_status"=>'20',"apply_uid"=>UID])){  //充值通过后
                throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
            }

            //添加订单日志
            if(!$this->getOrdersysOrderLogModel()->saveData($orderInfo,"10")){
                throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
            }
            $pay_name=$this->getMemberModel()->newQuery()->where("uid",UID)->value("nickname");
            $data = array(
                'order_id'=>$orderInfo->id,
                'member_id'=>$orderInfo->pay_uid,
                'member_name'=>$orderInfo->pay_username,
                'amount'=>$orderInfo->paid_amount,
                'sale_uid'=>$orderInfo->sale_id,
                'sale_name'=>$orderInfo->sale_name,
                'apply_uid'=>UID,
                'apply_name'=>$pay_name,
            );
            if(!$this->getOrdersysBalanceLogModel()->balanceLog("recharge",$data)){
                throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
            }
            //如果是有赠送金额则写赠送
            if($orderInfo->collected_amount>0){
                $data = array(
                    'order_id'=>$orderInfo->id,
                    'member_id'=>$orderInfo->pay_uid,
                    'member_name'=>$orderInfo->pay_username,
                    'amount'=>$orderInfo->collected_amount,
                    'sale_uid'=>$orderInfo->sale_id,
                    'sale_name'=>$orderInfo->sale_name,
                    'apply_uid'=>UID,
                    'apply_name'=>$pay_name,
                );
                if(!$this->getOrdersysBalanceLogModel()->balanceLog("recharge_send",$data)){
                    throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
                }
            }
            $adata=array();
            $adata["award_type"]=1;
            $adata["uid"]=$orderInfo->uid;
            $adata["order_id"]=$orderInfo->id;
            $adata['money']= $orderInfo->paid_amount;
            $adata["remark"]="支付订单号：".$orderInfo->id.",订单金额:".$orderInfo->all_amount."超过设定金额:";
            $this->getAwardLogModel()->awardLog(1,$adata);
            return array();
        }
    }

    /***
     * 审核失败
     * @param $data
     * @return mixed|void
     */
    public function applyError($data){
        if(!$this->getOrdersysOrderModel()->newQuery()->where("id",$data["id"])->update(["apply_status"=>'0',"apply_uid"=>UID,"apply_error_msg"=>$data['msg']])) {  //充值通过后
            throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        }
        return array();
    }

    /**
     * 用户退款
     * @param $data
     * @return mixed|void
     */
    public function backMoney($data){
        $orderInfo=$this->getOrdersysOrderModel()->newQuery()->where("id",$data["id"])->first();
        if($orderInfo->refund_state){
            throw new OrdersysException(OrdersysException::EROOR_BACK_MONEY_IEXT);
        }
        //一块退单数据
       return   $this->getOrdersysOrderModel()->newQuery()->where("id",$orderInfo->id)->update(["refund_state"=>1,'refund_amount'=>$data['refund_amount']]);
    }

    /**
     * 日志
     * @return AwardLogModel
     */
    public function getAwardLogModel(){
        return app()->make(AwardLogModel::class);
    }

    public function backApplyTrue($data){
        $orderInfo=$this->getOrdersysOrderModel()->newQuery()->where("id",$data["id"])->first();
        if(empty($orderInfo)){
           throw new  OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        }
        if(!$this->getOrdersysOrderModel()->newQuery()->where("id",$data["id"])->update(["refund_state"=>2,"apply_status"=>'30'])){
            throw new OrdersysException(OrdersysException::EROOR_ORDER_IS_NOT_IEXT);
        }
        //添加订单日志
        if(!$this->getOrdersysOrderLogModel()->saveData($orderInfo,"20")){
            throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        }
        if (!empty($orderInfo->pay_uid)) {   //支付定单不为空---用户金额是否不足
            $pay_name = $this->getMemberModel()->newQuery()->where("uid", UID)->value("nickname");
            $data = array(
                'order_id' => $orderInfo->id,
                'member_id' => $orderInfo->pay_uid,
                'member_name' => $orderInfo->pay_username,
                'amount' => $orderInfo->refund_amount,
                'sale_uid' => $orderInfo->sale_id,
                'sale_name' => $orderInfo->sale_name,
                'apply_uid' => UID,
                'apply_name' => $pay_name,
            );
            if (!$this->getOrdersysBalanceLogModel()->balanceLog("refund", $data)) {
                throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
            }
        }

        return array();
    }

    public function backApplyError($data){
        if(!$this->getOrdersysOrderModel()->newQuery()->where("id",$data["id"])->update(["refund_state"=>-1,'refund_msg_error'=>$data['msg']])){
            throw new OrdersysException(OrdersysException::EROOR_ORDER_IS_NOT_IEXT);
        }
        return array();
    }

    /**
     * 修改物流号
     * @param $data
     * @return mixed|void
     */
    public function changeShippingCode($data){
        if(!$this->getOrdersysOrderExpressModel()->newQuery()->where("order_id",$data["id"])->update(["shipping_status"=>1])){
            throw  new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        }
        return $this->getOrdersysOrderModel()->newQuery()->where("id",$data["id"])->update(["shipping_code"=>$data['content']]);
    }
}
